package com.frogs.ucenter.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.frogs.component.permission.api.service.PermissionHelper;
import com.frogs.component.permission.entity.User;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.component.RequestApiHelper;

/**
 * 短信
 *
 * @author lijian
 * @version 2014年6月19日 - 上午11:20:25
 */
@Controller
public class MessageController extends BaseController3 {
	
	private static final Logger log = LoggerFactory.getLogger(MessageController.class);

	@Resource(name = "permissionHelper")
	private PermissionHelper permissionHelper;
	
	@Resource
	private RequestApiHelper apiHelper;
	
	private String HOME_VIEW = "sms/index";
	
	@RequestMapping("/sms/index")
	public String index(HttpServletRequest request, ModelMap model) {
		setRequestModelMap(request, model);
		return HOME_VIEW;
	}
	
	@RequestMapping("/sms/send")
	public String sendMessage(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
		DWZResponse.Builder builder;
		String phone = reqInfo.getParameter("phone", "");
		String messageContent = reqInfo.getParameter("messageContent", "");
		if (!StringUtil.isMobilePhone(phone)) {
			builder = DWZResponse.getFailBuilder("手机号码有误");
			return null;
		}
		if (StringUtil.isEmpty(messageContent.trim())) {
			builder = DWZResponse.getFailBuilder("内容不能为空");
			return null;
		}
		if (messageContent.trim().length() > 500) {
			builder = DWZResponse.getFailBuilder("内容不能超过500字");
			return null;
		}
		
        try {
        	if (sendSms(phone, messageContent)) {
        		builder = DWZResponse.getSucessBuilder("发送成功");
    		} else {
    			builder = DWZResponse.getFailBuilder("发送失败");
    		}
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("发送失败：" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
	}
	
	private Boolean sendSms(String phone, String messageContent) {
		User user = permissionHelper.getUser();
		log.info("func[sendSms] user[{}] desc[send sms user]", new Object[] {user});
		JSONObject object = apiHelper.sendSms(phone, messageContent);
		if (ApiResultEnum.SUCCESS.getId() == object.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName())) {
			return true;
		} else {
			return false;
		}
	}
}
